package com.virus.coronavirus.controller;

import com.virus.coronavirus.error.SystemException;
import com.virus.coronavirus.model.Track;
import com.virus.coronavirus.model.vo.TrackVO;
import com.virus.coronavirus.response.ControllerEndpoint;
import com.virus.coronavirus.response.ResponseBean;
import com.virus.coronavirus.service.TrackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Bbeginner
 * @Date: 2021/2/5 18:51
 * @Version 1.0
 **/
@RestController
@RequestMapping("/virus/track")
@Validated
@Api(tags = "疫情防控模块-轨迹相关接口")
public class TrackController {

    @Autowired
    private TrackService trackService;

    //获得一个人所有的轨迹
    @ApiOperation(value = "获取一个人的轨迹")
    @RequiresPermissions({"track:findOnes"})
    @GetMapping("/findTracks{id}")
    public ResponseBean getTracksOfOne(@PathVariable Long id){
        List<Track> tracksOfOne = trackService.getTracksOfOne(id);
        return ResponseBean.success(tracksOfOne);
    }

    //获得单条轨迹
    @ApiOperation(value = "获取单条轨迹")
    @RequiresPermissions({"track:findOne"})
    @GetMapping("/findTrack{trackid}")
    public ResponseBean findonetrack(@PathVariable Long trackid){
        Track track = trackService.findonetrack(trackid);
        return ResponseBean.success(track);
    }

    /**
     * 添加轨迹信息
     * @param
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加轨迹失败", operation = "添加轨迹")
    @ApiOperation(value = "添加轨迹", notes = "添加轨迹信息")
    @RequiresPermissions({"track:add"})
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated TrackVO trackVO) throws SystemException {
        trackService.add(trackVO);
        return ResponseBean.success();
    }
    /*public ResponseBean add( Long userId,Double lng,Double lat,String time,String track_name) throws SystemException {
        Track track = trackService.add(userId, lng, lat, time,track_name);
        return ResponseBean.success(track);
    }*/

    //查找一个人在daytime内的轨迹
    @ApiOperation(value = "查询一个人最近几天内轨迹", notes = "通过id查询几天内的轨迹")
    @RequiresPermissions({"track:find"})
    @GetMapping("/findonestracksinrecentdays")
    public ResponseBean findOnesTracksLastDays(Long id,Integer day) throws SystemException {
        List<Track> trackById = trackService.findTrackById(id, day);
        return ResponseBean.success(trackById);
    }

    //查找一个人在time1至time2内的轨迹
    @ApiOperation(value = "查找一个人在某时间范围内的轨迹", notes = "通过一段时间内的轨迹，以天为单位")
    @RequiresPermissions({"track:find"})
    @PostMapping("/idtime1time2")
    public ResponseBean findOnesTracksFormTime1ToTime2(Long id,String time1,String time2) throws Exception {
        List<Track> trackByTime = trackService.findTrackByTime(id, time1,time2);
        return ResponseBean.success(trackByTime);
    }

    //获得在一段时间内的所有人的轨迹
    @ApiOperation(value = "查找time1至time2内的所有轨迹",notes = "获取一段时间内的轨迹")
    @RequiresPermissions({"track:findTrackstime"})
    @PostMapping("/findTrackstime")
    public ResponseBean getTracksByTime(String time1,String time2) throws Exception {
        List<Track> everyonesTrackByTime = trackService.findEveryonesTrackByTime(time1, time2);
        return ResponseBean.success(everyonesTrackByTime);
    }

    //获得一个人一天内的轨迹
    @ApiOperation(value = "查找一个人某一天的轨迹", notes = "通过一个人一天的轨迹")
    @RequiresPermissions({"track:findonestrack"})
    @PostMapping("/idtime")
    public ResponseBean findOnesTrackSomeday(Long id,String time) throws Exception {
        List<Track> trackByTime = trackService.findOnesTrackSomeday(id, time);
        return ResponseBean.success(trackByTime);
    }

    //更新（编辑）某条轨迹信息
    @ControllerEndpoint(exceptionMessage = "更新信息失败", operation = "更新轨迹信息")
    @ApiOperation(value = "更新轨迹", notes = "更新轨迹信息")
    @RequiresPermissions({"track:update"})
    @PutMapping("/update/{trackid}")
    public ResponseBean update(@PathVariable Long trackid, Double lat, Double lng, String time, String trackname) throws Exception {
        Track track = trackService.update(trackid, lat, lng, time, trackname);
        return ResponseBean.success(track);
    }


    /**
     * 获得范围内的所有人的轨迹
     * @param lng1
     * @param lat1
     * @param lng2
     * @param lat2
     */
    @ApiOperation(value = "获取范围内的轨迹",notes = "获取经过某地区所有轨迹")
    @RequiresPermissions({"track:findTracks"})
    @GetMapping("/findTracksplace")
    public ResponseBean getTracksByPlace(Double lng1, Double lat1, Double lng2, Double lat2) throws Exception {
        List<Track> trackplaceall = trackService.trackplaceall(lng1, lat1, lng2, lat2);
        return ResponseBean.success(trackplaceall);
    }

    /**
     * 通过Id找某范围内的轨迹
     * @param id
     * @param lng1
     * @param lat1
     * @param lng2
     * @param lat2
     */
    @ApiOperation(value = "查找一个人在某经纬度范围内的轨迹", notes = "获取范围内的轨迹,配合用户Id")
    @RequiresPermissions({"track:find"})
    @GetMapping("/idplace")
    public ResponseBean findOnesTracksByplace(Long id,Double lng1, Double lat1, Double lng2, Double lat2) throws Exception {
        List<Track> trackplaceid = trackService.trackplaceid(id, lng1, lat1, lng2, lat2);
        return ResponseBean.success(trackplaceid);
    }

    @ApiOperation(value = "查找在某经纬度范围内指定一天的轨迹", notes = "获取范围内的轨迹,配合时间")
    @RequiresPermissions({"track:find"})
    @GetMapping("/timeplace")
    public ResponseBean findTracksBytimeplace(String time,Double lng1, Double lat1, Double lng2, Double lat2) throws Exception {
        List<Track> tracktimeplace = trackService.tracktimeplace(time, lng1, lat1, lng2, lat2);
        return ResponseBean.success(tracktimeplace);
    }

    @ApiOperation(value = "查找在某经纬度范围内指定时间范围的轨迹", notes = "获取范围内的轨迹,配合时间")
    @RequiresPermissions({"track:find"})
    @GetMapping("/time1time2place")
    public ResponseBean findTracksBytime1time2place(String time1,String time2,
                                                    Double lng1, Double lat1, Double lng2, Double lat2) throws Exception {
        List<Track> tracktimeplace = trackService.tracktime1time2place(time1,time2, lng1, lat1, lng2, lat2);
        return ResponseBean.success(tracktimeplace);
    }
}
